﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using GqqMvcWeb.Models.Database;

namespace GqqMvcWeb.Controllers
{ 
    public class StudentController : Controller
    {
        private readonly SchoolContext db = new SchoolContext();

        //
        // GET: /Student/

        public ViewResult Index()
        {
//            var students = new List<student>
//                               {
//                                   new student { FirstMidName = "张三", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01")},
//                                   new student {StudentID=200, FirstMidName = "张三", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01")},
//                                   new student {StudentID=201, FirstMidName = "张三", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01")},
//                                   //new student {FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01")},
//                                   //new student {FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01")},
//                                   //new student {FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01")},
//                                   //new student {FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01")},
//                                   //new student {FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01")},
//                                   //new student {FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01")},
//                                   //new student {FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01")}
//                               };
//            students.ForEach(s => db.Students.Add(s));
//            db.SaveChanges();
//            db.Database.CreateIfNotExists();
//            const string sql = "select FirstName as Ming,LastName as Xing from student where studentid={0};";
            //db.Database.SqlQuery<StudentViewModel>(sql,1).ToList();
//            CodeLogManager.DLog.Debug(counts[0]);
            return View(db.Students.ToList());
        }

        //
        // GET: /Student/Details/5

        public ViewResult Details(int id)
        {
            student student = db.Students.Find(id);
            return View(student);
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(student student)
        {
            if (ModelState.IsValid)
            {
                db.Configuration.ValidateOnSaveEnabled = false;
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(student);
        }
        
        //
        // GET: /Student/Edit/5
 
        public ActionResult Edit(int id)
        {
            student student = db.Students.Find(id);
            return View(student);
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Student/Delete/5

        public ActionResult Delete(int id, bool? hasError, string err)
        {
            student student = db.Students.Find(id);
            if (hasError.HasValue && hasError.Value)
                ViewBag.ErrMessage = err;
            return View(student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
//                student student = db.Students.Find(id);
//                throw new Exception();
//                db.Students.Remove(student);

                // 表示使用这种方法,可以直接生成通过主键删除的sql,是一个很不错的方法呢。
                student stu = new student {StudentID = id};
                db.Entry(stu).State = EntityState.Deleted;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                return RedirectToAction("Delete", new System.Web.Routing.RouteValueDictionary {{"hasError", true}, {"err", ex.Message}});
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}